package cn.kgc.shiro.web.controller;

import cn.kgc.shiro.web.entity.User;
import cn.kgc.shiro.web.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 课工场
 * @date 2024/12/6
 * @description
 */
@RestController
@RequestMapping("user")
public class UserController {


    @Autowired
    private UserService userService;

    @GetMapping("list")
    public List<User> userList() {
        return userService.list();
    }


    // 受限资源
    @GetMapping("delete")
    // @RequiresRoles("admin1")
    @RequiresPermissions("sys:user:delete")
    public Map<String, Object> removeUser(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();
        // if (subject.hasRole("admin1")){
        //     boolean b = userService.removeById(id);
        //     map.put("code",0);
        //     map.put("message","删除成功");
        // }else {
        //     map.put("code",403);
        //     map.put("message","没有权限,请联系管理员");
        // }

        boolean b = userService.removeById(id);
        map.put("code", 0);
        map.put("message", "删除成功");

        return map;
    }

    @GetMapping("add")
    public Map<String, Object> addUser(User user) {
        HashMap<String, Object> map = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();
        if (subject.hasRole("super")) {
            map.put("code", 0);
            map.put("message", "添加成功");
        } else {
            map.put("code", 403);
            map.put("message", "没有权限,请联系管理员");
        }
        return map;
    }

    @GetMapping("toLogin")
    public Map<String, Object> toLogin() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 401);
        result.put("message", "请认证后访问...");
        return result;
    }


    @GetMapping("login")
    public Map<String, Object> login(User user) {

        HashMap<String, Object> result = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();

        try {
            subject.login(new UsernamePasswordToken(user.getUsername(), user.getPassword()));
            result.put("code", 0);
            result.put("message", "认证通过");
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            result.put("code", 110);
            result.put("message", "用户名不正确");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            result.put("code", 110);
            result.put("message", "密码不正确");
        }
        return result;
    }
}
